<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class commission_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//写入佣金表
	public function add($member_id, $parent_id, $type=1) {
		//订单佣金
		$status = 0;
		$memo = "";
		if ($type==1) {
			$memo = "订单佣金收入";
			$sql = "SELECT total_price FROM jf_order WHERE id='{$parent_id}'";
			$order_price = floatval($this->db->get_var($sql));
			$commission = $this->calculate($order_price, 1);
		} elseif ($type==2) {
			$memo = "邀请用户注册收入";
			$commission = $this->calculate(0, 2);
			$status = 1;
		}
		$sql = "INSERT INTO jf_member_commission(member_id, parent_id, commission, commission_type, memo, status, add_time) 
			VALUES('{$member_id}', '{$parent_id}', '{$commission}', '{$type}', '{$memo}', '{$status}', '".time()."')";
		$this->db->query($sql);
	}

	//结算佣金
	public function jiesuan($parent_id, $type=1){
		$commission = 0;
		$sql = "SELECT * FROM jf_member_commission WHERE parent_id='{$parent_id}' AND type='{$type}'";
		$row = $this->db->get_row($sql);
		if ($row) {
			if ($row->status!=2) {
				$commission = $row->commission;
				//更新状态
				$sql = "UPDATE jf_member_commission SET status='2' WHERE id='{$row->id}'";
				$this->db->query($sql);
				//更新用户的佣金
				$sql = "UPDATE jf_member SET commission=commission+{$row->commission} WHERE id='{$row->member_id}'";
				$this->db->query($sql);
			}
		}
		return $commission;
	}

	//取消佣金
	public function cancel($parent_id, $type=1){
		$commission = 0;
		$sql = "SELECT * FROM jf_member_commission WHERE parent_id='{$parent_id}' AND commission_type='{$type}'";
		$row = $this->db->get_row($sql);
		if ($row) {
			$commission = $row->commission;
			//原本是已经结算的状态，需要减去
			if ($row->status==1) {
				$sql = "SELECT commission FROM jf_member WHERE id='{$row->member_id}'";
				$member_commission = $this->db->get_var($sql);
				$new_commission = $member_commission - $commission;
				//允许负数的佣金出现
				//if ($new_commission < 0) $new_commission = 0;
				//更新用户的佣金
				$sql = "UPDATE jf_member SET commission={$new_commission} WHERE id='{$row->member_id}'";
				$this->db->query($sql);
				//更新状态
				$sql = "UPDATE jf_member_commission SET status='-1' WHERE id='{$row->id}'";
				$this->db->query($sql);
			}else{
				//更新状态
				$sql = "UPDATE jf_member_commission SET status='-1' WHERE id='{$row->id}'";
				$this->db->query($sql);
			}
		}
		return $commission;
	}

	//计算佣金 type=1：订单佣金
	public function calculate($money, $type=1) {
		$commission = 0;
		switch ($type) {
			case 1:
				$sql = "SELECT content FROM jf_config WHERE name='invite_order_commission'";
				$percent = floatval($this->db->get_var($sql));
				if ($percent) $commission = number_format($money * $percent, 2);
				break;
			case 2:
				//注册的佣金是固定的金额，不是百分比
				$sql = "SELECT content FROM jf_config WHERE name='invite_register_commission'";
				$commission = floatval($this->db->get_var($sql));
				//if ($percent) $commission = number_format($money * $percent / 100, 2);
				break;
		}
		return $commission;
	}

	//状态
	public function status_name($status) {
		$str = $status;
		switch($status){
			case 0:$str = '隐藏';break;
			default:$str = '正常';break;
		}
		return $str;
	}	
	//转到可用佣金
	public function turn_commission($order_id) {
		$sql ="SELECT o.* ,m.parent_id as parent_member_id FROM jf_order o LEFT JOIN jf_member m ON o.member_id = m.id WHERE o.id = $order_id";
		$order = $this->db->get_row($sql);
		if($order->status > 2){
			//查出佣金多少
			$sql = "SELECT id, commission ,member_id FROM jf_member_commission WHERE member_id = {$order->parent_member_id} 
					AND status = 0 AND commission_type = 1 AND parent_id = {$order_id}";
			$commission = $this->db->get_row($sql);
			if($commission){
				//佣金转到会员表
				$sql = "UPDATE jf_member SET commission = commission + {$commission->commission} 
						WHERE id = {$commission->member_id}";
				$rs = $this->db->query($sql);
				if($rs){ 
					//更新佣金状态
					$sql = "UPDATE jf_member_commission SET status = 1 WHERE id = {$commission->id}";
					$this->db->query($sql);
				}
			}
		}
		return ;
	}
}
